//
//  HJGroupRemoveManagerController.m
//  KaiChat
//
//  Created by lx on 2023/11/8.
//

#import "HJGroupRemoveManagerController.h"
#import "HJGroupSelectMemberCell.h"
#import "HJNavigationController.h"
#import "TJEmptyDataHandler.h"
#import "HJIM.h"
#import "HJUserInfoController.h"

@interface HJGroupRemoveManagerController ()

@property (nonatomic, strong) NSMutableArray *datas;
@property (nonatomic, strong) NSMutableArray *selectDatas;
@property (nonatomic, weak) HJBarButton *doneButton;
@property (nonatomic, strong) TJEmptyDataHandler *emptyDataHandler;


@end

@implementation HJGroupRemoveManagerController


- (void)showInController:(UIViewController *)controller {
    HJNavigationController *navController = [[HJNavigationController alloc] initWithRootViewController:self];
    [controller presentViewController:navController animated:YES completion:nil];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    _emptyDataHandler = [TJEmptyDataHandler emptyDataHandleWithScrollView:self.tableView];
    
    HJBarButton *doneButton = [HJBarButton barButtonWithTitle:@"Done" color:UIColor.tj_mainColor target:self action:@selector(didClickDoneButton:)];
    doneButton.enabled = NO;
    [doneButton setTitleColor:UIColor.tj_mainColor forState:UIControlStateNormal];
    [doneButton setTitleColor:UIColor.tj_BEBDCAColor forState:UIControlStateDisabled];
    [self.navigationBar.contentView addSubview:doneButton];
    [doneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.inset(leftRightMargin);
        make.width.offset(40);
        make.centerY.offset(0);
        make.height.offset(40);
    }];
    _doneButton = doneButton;
    
    self.navigationBar.title = @"Cancel Administrator";
    
    self.view.backgroundColor = UIColor.whiteColor;
    self.tableViewStyle = UITableViewStylePlain;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, TabbarSafeBottomMargin, 0);
    [self.tableView registerClass:HJGroupSelectMemberCell.class forCellReuseIdentifier:@"HJGroupSelectMemberCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = 64;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView setSectionIndexColor:UIColor.tj_9B9B9BColor];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(KNavBarHight);
        make.left.right.bottom.offset(0);
    }];
    
    [self loadData];
}

- (void)loadData {
    [[HJIM shareIM].groupManager queryGroupMemberListWithGroupId:self.groupId completion:^(NSArray<HJGroupMemberModel *> * _Nullable datas) {
        for (HJGroupMemberModel *obj in datas) {
            if (obj.role == 2) {
                [self.datas addObject:obj];
            }
        }
        
        dispatch_async_main_queue(^{
            self.emptyDataHandler.canDisplay = YES;
            [self.tableView reloadData];
        });
    }];
}

 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJGroupSelectMemberCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HJGroupSelectMemberCell" forIndexPath:indexPath];
    HJGroupMemberModel *model = self.datas[indexPath.row];
    model.isLookInfo = YES;
    cell.model = model;
    TJWeakify(self)
    [cell setDidClickInfoButtonBlock:^{
        HJUserInfoController *infoController = [[HJUserInfoController alloc] init];
        infoController.userId = model.userId;
        [weakself.navigationController pushViewController:infoController];
    }];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HJGroupMemberModel *model = self.datas[indexPath.row];
    
    model.isSelect = !model.isSelect;
    
    HJGroupSelectMemberCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.model = model;
    if (model.isSelect) {
        if (![self.selectDatas containsObject:model]) {
            [self.selectDatas addObject:model];
        }
    } else {
        if ([self.selectDatas containsObject:model]) {
            [self.selectDatas removeObject:model];
        }
    }
    [self updateDoneTitle];
}


- (void)didClickDoneButton:(UIButton *)button {
    
    [HJHUD showLoadingInView:self.view];
    
    NSMutableArray *array = [NSMutableArray array];
    for (HJGroupMemberModel *model in self.selectDatas) {
        [array addObject:model.userId];
    }
        
    [HJIM.shareIM.groupManager removeGroupManagerByGroupId:self.groupId userIds:array completion:^(NSError * _Nullable error) {
        if (error)  {
            [HJHUD showErrorMessageTip];
            return;
        }
        
        if (self.complete) {
            self.complete();
        }
        [self dismissToRootViewController:YES completion:nil];
        [HJHUD showSucceedMessageTip];
    }];
 
}

- (void)updateDoneTitle {
    if (self.selectDatas.count > 0) {
        _doneButton.enabled = YES;
        [_doneButton setTitle:[NSString stringWithFormat:@"Done(%ld)", self.selectDatas.count] forState:UIControlStateNormal];
    } else {
        _doneButton.enabled = NO;
        [_doneButton setTitle:@"Done" forState:UIControlStateNormal];
    }
    [_doneButton sizeToFit];
    CGFloat doneButtonW = MAX(_doneButton.tj_width + 10, 40);
    [_doneButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.offset(doneButtonW);
    }];
}

- (NSMutableArray *)datas {
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}

- (NSMutableArray *)selectDatas {
    if (!_selectDatas) {
        _selectDatas = [NSMutableArray array];
    }
    return _selectDatas;
}

- (UIModalPresentationStyle)modalPresentationStyle {
    return UIModalPresentationFullScreen;
}


@end
